package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.SecondHandMapper;
import com.bbt.dormassistant.entity.SecondHand;
import com.bbt.dormassistant.service.SecondHandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class SecondHandServiceImpl implements SecondHandService {


    @Resource
    private SecondHandMapper secondHandMapper;


    /**
     *
     * @param userId 用户ID
     * @param title 标题
     * @param content 内容
     * @return Boolean
     */
    @Override
    public Boolean addSecondHand(String userId, String title, String content) {

        SecondHand secondHand = new SecondHand();
        secondHand.setUserId(userId);
        secondHand.setTitle(title);
        secondHand.setContent(content);
        secondHand.setTime(new Date());


        return secondHandMapper.insert(secondHand) == 1;

    }


    /**
     *
     * @param secondHandId 需要查询的ID
     * @return SecondHand
     */
    @Override
    public SecondHand queryBySecondHandId(Integer secondHandId) {
        return secondHandMapper.selectByPrimaryKey(secondHandId);
    }

    /**
     *
     * @param currIndex 当前页
     * @param pageSize 页大小
     * @return List<SecondHand>
     */
    @Override
    public List<SecondHand> queryAll(Integer currIndex,Integer pageSize) {
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",currIndex==null? 0:(currIndex-1)*pageSize );
        data.put("pageSize",pageSize==null? 999:pageSize);


        return secondHandMapper.selectAll(data);
    }
}
